package com.hydl.power.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.mapper.MaxDataMapper;
import com.hydl.power.pojo.ExtremeAnalyze;
import com.hydl.power.pojo.Identical;
import com.hydl.power.service.ExtremeAnalyzeService;
import com.hydl.power.service.MaxDataService;
import com.hydl.power.utils.EasyPoiUtil;
import com.hydl.power.utils.Resp;
import com.hydl.power.utils.TableUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author wxb
 * @date 2019/12/30 16:01
 */
@RestController
@RequestMapping("/maxData")
@Api(value = "MaxDataController",tags = "最值分析")
@CrossOrigin
public class MaxDataController {
    @Autowired
    private MaxDataService maxDataService;

    @Autowired
    private MaxDataMapper maxDataMapper;
    @Autowired
    private ExtremeAnalyzeService extremeAnalyzeService;

    // @LogAnnotation(module = "最值分析",description = "最值分析list分页")
    @PostMapping(value = "/getAllMaxDataByPage")
    @ApiOperation(value = "最值分析list分页",produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数",required = false,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数",required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "区域", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房名称", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "dataType", value = "数据类型 小时 ,天,月", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String"),
    })
    public Resp getAllLoad(Integer pageNum, Integer pageSize, String cityId, String projectId, String roomId, String dataType, String startTime, String endTime, String token){
        if(dataType=="day" || dataType.equals("day")){
            return Resp.success(extremeAnalyzeService.queryByHour( pageNum,  pageSize, cityId, projectId, roomId, token, startTime,endTime));
        }
        if(dataType=="month" || dataType.equals("month")){
            return Resp.success(extremeAnalyzeService.queryBydayStart( pageNum,pageSize, cityId, projectId, roomId, token, startTime,endTime));
        }
        if(dataType=="year" || dataType.equals("year")){
            return Resp.success(extremeAnalyzeService.queryByMonthStart( pageNum,pageSize, cityId, projectId, roomId, token, startTime,endTime));
        }
        return null;
    }


    // @LogAnnotation(module = "最值分析",description = "最值分析导出")
    @GetMapping(value = "excelMaxData")
    @ApiOperation(value = "最值分析导出")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数",  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "区域", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房名称", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "dataType", value = "数据类型 小时 ,天,月", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String"),
    })
    public void IdenticalExport(HttpServletResponse response,Integer pageNum, Integer pageSize, String cityId, String projectId, String roomId, String dataType, String startTime, String endTime, String token) throws IOException {
        String fileName="";
        PageInfo<ExtremeAnalyze> list=new PageInfo<>();

        if(dataType=="day" || dataType.equals("day")){
            fileName="日最值分析";
             list= extremeAnalyzeService.queryByHour( pageNum,  pageSize, cityId, projectId, roomId, token, startTime,endTime);
        }
        if(dataType=="month" || dataType.equals("month")){
            fileName="月最值分析";
             list= extremeAnalyzeService.queryBydayStart( pageNum,pageSize, cityId, projectId, roomId, token, startTime,endTime);
        }
        if(dataType=="year" || dataType.equals("year")){
            fileName="年最值分析";
             list= extremeAnalyzeService.queryByMonthStart( pageNum,pageSize, cityId, projectId, roomId, token, startTime,endTime);
        }
        List<ExtremeAnalyze> arr = list.getList();
        EasyPoiUtil.exportExcel(EasyPoiUtil.getWorkbook(fileName, fileName, ExtremeAnalyze.class, arr), fileName, response);
    }

}
